package com.frxuexi.mdm2204.demo

import android.util.Log
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material.icons.filled.ArrowForward
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.Divider
import androidx.compose.material3.Icon
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.frxuexi.mdm2204.R

@Preview
@Composable
fun Demo06() {
    var num by remember {
        mutableStateOf(1)
    }
    var num2 by remember {
        mutableStateOf(1)
    }
    var num3 by remember {
        mutableStateOf(1)
    }

    Scaffold (bottomBar = {
        Divider()
        Row(modifier = Modifier
            .padding(horizontal = 10.dp)
            .fillMaxWidth(),verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.SpaceBetween) {
            Row {
                Image(
                    painter = painterResource(id = R.drawable.ic_action_check),
                    contentDescription = ""
                )
                Text(text = "全选")
            }
            Row(verticalAlignment = Alignment.CenterVertically) {
                Text(text = "合计：")
                Text(text = "￥150")
                Spacer(modifier = Modifier.width(8.dp))
                Button(colors =ButtonDefaults.buttonColors(containerColor = Color.Red) , onClick = { }) {
                    Text(text = "去结算（3）")
                }
            }
        }
    }){
        Column(
            Modifier
                .padding(it)
                .fillMaxSize()
                .background(colorResource(id = R.color.lightgray))
        ) {
            Row(
                Modifier
                    .fillMaxWidth()
                    .padding(10.dp),
                verticalAlignment = Alignment.CenterVertically,
                horizontalArrangement = Arrangement.SpaceBetween
            ) {
                TextButton(
                    colors = ButtonDefaults.textButtonColors(contentColor = Color.Black),
                    onClick = { /*TODO*/ }) {
                    Icon(imageVector = Icons.Default.ArrowBack, contentDescription = "")
                    Text(text = "购物车")
                }
                Text(text = "编辑")
            }

            Card(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(horizontal = 10.dp),
                colors = CardDefaults.cardColors(containerColor = Color.White)
            ) {
                Column(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(10.dp), verticalArrangement = Arrangement.spacedBy(10.dp)
                ) {
                    Row(horizontalArrangement = Arrangement.spacedBy(5.dp)) {
                        Image(
                            painter = painterResource(id = R.drawable.ic_action_check),
                            contentDescription = ""
                        )
                        Text(text = "森巴舞巴西烧肉店...")
                        Icon(imageVector = Icons.Default.ArrowForward, contentDescription = "")
                    }
                    Row(horizontalArrangement = Arrangement.spacedBy(5.dp)) {
                        Image(
                            painter = painterResource(id = R.drawable.ic_action_check),
                            contentDescription = "",
                            Modifier.height(80.dp)
                        )
                        Image(
                            painter = painterResource(id = R.drawable.ic_launcher_background),
                            contentDescription = "",
                            Modifier.size(80.dp)
                        )
                        Column(Modifier.height(80.dp), verticalArrangement = Arrangement.SpaceBetween) {
                            Row(
                                modifier = Modifier
                                    .fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween
                            ) {
                                Text(text = "森巴舞巴西烧肉两人餐")
                                Text(text = "￥85")
                            }

                            Text(text = "限购10件", color = Color.Red, fontSize = 12.sp)
                            Row(
                                modifier = Modifier
                                    .height(25.dp)
                                    .border(
                                        1.dp, Color.Gray,
                                        RoundedCornerShape(5.dp)
                                    ), verticalAlignment = Alignment.CenterVertically
                            ) {


                                Text(
                                    text = "-", modifier = Modifier
                                        .width(30.dp)
                                        .clickable {
                                            if (num > 1) {
                                                num--
                                            } else {
                                                Log.i("===", "购买数不能小于1")
                                            }
                                        }, textAlign = TextAlign.Center
                                )
                                Divider(
                                    Modifier
                                        .width(1.dp)
                                        .fillMaxHeight()
                                )
                                Text(
                                    text = "$num",
                                    fontSize = 12.sp,
                                    modifier = Modifier.padding(horizontal = 10.dp)
                                )
                                Divider(
                                    Modifier
                                        .width(1.dp)
                                        .fillMaxHeight()
                                )
                                Text(
                                    text = "+",
                                    Modifier
                                        .width(30.dp)
                                        .clickable { num++ },
                                    textAlign = TextAlign.Center
                                )
                            }
                        }


                    }
                    Row(horizontalArrangement = Arrangement.spacedBy(5.dp)) {
                        Image(
                            painter = painterResource(id = R.drawable.ic_action_check),
                            contentDescription = "",
                            Modifier.height(80.dp)
                        )
                        Image(
                            painter = painterResource(id = R.drawable.ic_launcher_background),
                            contentDescription = "",
                            Modifier.size(80.dp)
                        )
                        Column(Modifier.height(80.dp), verticalArrangement = Arrangement.SpaceBetween) {
                            Row(
                                modifier = Modifier
                                    .fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween
                            ) {
                                Text(text = "森巴舞巴西烧肉单人餐")
                                Text(text = "￥35")
                            }

                            Text(text = "限购10件", color = Color.Red, fontSize = 12.sp)
                            Row(
                                modifier = Modifier
                                    .height(25.dp)
                                    .border(
                                        1.dp, Color.Gray,
                                        RoundedCornerShape(5.dp)
                                    ), verticalAlignment = Alignment.CenterVertically
                            ) {


                                Text(
                                    text = "-", modifier = Modifier
                                        .width(30.dp)
                                        .clickable {
                                            if (num2 > 1) {
                                                num2--
                                            } else {
                                                Log.i("===", "购买数不能小于1")
                                            }
                                        }, textAlign = TextAlign.Center
                                )
                                Divider(
                                    Modifier
                                        .width(1.dp)
                                        .fillMaxHeight()
                                )
                                Text(
                                    text = "$num2",
                                    fontSize = 12.sp,
                                    modifier = Modifier.padding(horizontal = 10.dp)
                                )
                                Divider(
                                    Modifier
                                        .width(1.dp)
                                        .fillMaxHeight()
                                )
                                Text(
                                    text = "+",
                                    Modifier
                                        .width(30.dp)
                                        .clickable { num2++ },
                                    textAlign = TextAlign.Center
                                )
                            }
                        }


                    }

                }


            }
            Spacer(modifier = Modifier.height(10.dp))
            Card(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(horizontal = 10.dp),
                colors = CardDefaults.cardColors(containerColor = Color.White)
            ) {
                Column(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(10.dp), verticalArrangement = Arrangement.spacedBy(10.dp)
                ) {
                    Row(horizontalArrangement = Arrangement.spacedBy(5.dp)) {
                        Image(
                            painter = painterResource(id = R.drawable.ic_action_check),
                            contentDescription = ""
                        )
                        Text(text = "森巴舞巴西烧肉店...")
                        Icon(imageVector = Icons.Default.ArrowForward, contentDescription = "")
                    }
                    Row(horizontalArrangement = Arrangement.spacedBy(5.dp)) {
                        Image(
                            painter = painterResource(id = R.drawable.ic_action_check),
                            contentDescription = "",
                            Modifier.height(80.dp)
                        )
                        Image(
                            painter = painterResource(id = R.drawable.ic_launcher_background),
                            contentDescription = "",
                            Modifier.size(80.dp)
                        )
                        Column(Modifier.height(80.dp), verticalArrangement = Arrangement.SpaceBetween) {
                            Row(
                                modifier = Modifier
                                    .fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween
                            ) {
                                Text(text = "森巴舞巴西烧肉两人餐")
                                Text(text = "￥85")
                            }

                            Text(text = "限购10件", color = Color.Red, fontSize = 12.sp)
                            Row(
                                modifier = Modifier
                                    .height(25.dp)
                                    .border(
                                        1.dp, Color.Gray,
                                        RoundedCornerShape(5.dp)
                                    ), verticalAlignment = Alignment.CenterVertically
                            ) {


                                Text(
                                    text = "-", modifier = Modifier
                                        .width(30.dp)
                                        .clickable {
                                            if (num3 > 1) {
                                                num3--
                                            } else {
                                                Log.i("===", "购买数不能小于1")
                                            }
                                        }, textAlign = TextAlign.Center
                                )
                                Divider(
                                    Modifier
                                        .width(1.dp)
                                        .fillMaxHeight()
                                )
                                Text(
                                    text = "$num3",
                                    fontSize = 12.sp,
                                    modifier = Modifier.padding(horizontal = 10.dp)
                                )
                                Divider(
                                    Modifier
                                        .width(1.dp)
                                        .fillMaxHeight()
                                )
                                Text(
                                    text = "+",
                                    Modifier
                                        .width(30.dp)
                                        .clickable { num3++ },
                                    textAlign = TextAlign.Center
                                )
                            }
                        }

                    }
                }
            }
            Spacer(modifier = Modifier.height(10.dp))
            Text(
                text = "为/您/推/荐",
                fontWeight = FontWeight.Bold,
                textAlign = TextAlign.Center,
                modifier = Modifier.fillMaxWidth()
            )
            Spacer(modifier = Modifier.height(10.dp))
            Row(Modifier.padding(10.dp), horizontalArrangement = Arrangement.spacedBy(10.dp)) {
                Box(contentAlignment = Alignment.TopEnd, modifier = Modifier.weight(1f)) {
                    Image(
                        painter = painterResource(id = R.drawable.ic_launcher_background),
                        contentScale = ContentScale.Crop,
                        contentDescription = "",
                        modifier = Modifier
                            .fillMaxWidth()
                            .clip(
                                RoundedCornerShape(20.dp)
                            )
                    )
                    Text(
                        text = "1.4km",
                        color = Color.White,
                        modifier = Modifier
                            .offset(-10.dp, 10.dp)
                            .alpha(0.7f)
                            .background(Color.Gray, RoundedCornerShape(5.dp))
                            .padding(5.dp)
                    )
                }
                Box(contentAlignment = Alignment.TopEnd, modifier = Modifier.weight(1f)) {
                    Image(
                        painter = painterResource(id = R.drawable.ic_launcher_background),
                        contentScale = ContentScale.Crop,
                        contentDescription = "",
                        modifier = Modifier
                            .fillMaxWidth()
                            .clip(
                                RoundedCornerShape(20.dp)
                            )
                    )
                    Text(
                        text = "1.4km",
                        color = Color.White,
                        modifier = Modifier
                            .offset(-10.dp, 10.dp)
                            .alpha(0.7f)
                            .background(Color.Gray, RoundedCornerShape(5.dp))
                            .padding(5.dp)
                    )
                }
            }
        }
    }


}